<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/7
 * Time: 9:51
 */

namespace App\FreeBird\Repositories;

use App\FreeBird\Contracts\CollectionInterface;
use App\FreeBird\Models\CollectionModel;

class  DbCollectionRepositories implements CollectionInterface
{


    /**
     * 用户收藏
     * @return CollectionModel
     */
    public function getCollectionModel()
    {
        return app()->make(CollectionModel::class);
    }

    /**
     * 用户收藏
     * @param $data
     */
    public function setCollection($data)
    {
        $info = $this->getCollectionModel()->newQuery()->where("uid", AUID)->where("collection_id", $data["collection_id"])->where("collection_type", $data['collection_type'])->first();
        if ($info) {
            return $this->getCollectionModel()->newQuery()->where("id", $info->id)->delete();
        }
        $saveData = array();
        $saveData["collection_id"] = $data["collection_id"];
        $saveData["collection_type"] = $data["collection_type"];
        $saveData["uid"] = AUID;
        return $this->getCollectionModel()->newQuery()->create($saveData);
    }

    /**
     * 获取收藏类型
     * @param $data
     */
    public function getCollection($data){
        $collectionType=$data["collection_type"];
        if($collectionType==1){
            return $this->getCollectionModel()->newQuery()
                 ->with(["wxuser"])
                ->where("uid",AUID)
                ->where("collection_type",$collectionType)
                ->paginate(10);
        }else{
            return $this->getCollectionModel()->newQuery()
                ->with(["store"])
                ->where("uid",AUID)
                ->where("collection_type",$collectionType)
                ->paginate(10);
        }
    }
}